using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SysTables
{
    public partial class MainWindow : Form
    {

        private AddDatabaseForm mAddDatabaseForm;
        private AddTableForm mAddTableForm;
        private AddAttributeForm mAddAttributeForm;
        private AlterAttributeForm mAlterAttributeForm;

        Sysdatabases mSysdatabases;
        public MainWindow()
        {
            InitializeComponent();
            mSysdatabases = new Sysdatabases(false);
            mAddDatabaseForm = new AddDatabaseForm(this);
            mAddTableForm = new AddTableForm(this);
            mAddAttributeForm = new AddAttributeForm(this);
            mAlterAttributeForm = new AlterAttributeForm();
            ReloadAll();
        }


        public Database SelectedDatabases()
        {
            return mSysdatabases.FindDatabase(((String)mDatabases.SelectedItem));
        }

        public void ReloadDatabases()
        { 
            mDatabases.Items.Clear();
            foreach (Database database in mSysdatabases.Databases)
	        {
                mDatabases.Items.Add(database.Name);
	        }
            if (mDatabases.Items.Count>0) mDatabases.SetSelected(0, true);
        }

        public Table SelectedTable()
        {
            Database database = mSysdatabases.OpenedDatabase;
            if (database == null) return null;
            return database.FindTable((string)mTables.SelectedItem);
        }

        public void ReloadTables()
        {
            mTables.Items.Clear();
            if (mSysdatabases.OpenedDatabase == null) return ;
            foreach (Table tab in mSysdatabases.OpenedDatabase.Tables)
            {
                mTables.Items.Add(tab.TableName);

            }
            if (mTables.Items.Count > 0) mTables.SetSelected(0, true);
        }

        public Attribute SelectedAttribute()
        {
            Table tab = SelectedTable();
            if (tab == null) return null;

            if (mAttributesGrid.SelectedRows.Count > 0)
            {
                string nazov = (string)mAttributesGrid.SelectedRows[0].Cells[0].Value;
                return tab.FindAttr(nazov);
            }
            return null;

            
        }

        public void ReloadAttributes()
        {

            object[] values = new object[4];
            mAttributesGrid.Rows.Clear();

            if (SelectedTable() == null)          
                return;
            foreach (Attribute attr in SelectedTable().Attributes)
            {
                values[0] = attr.Name;
                values[1] = attr.Type.ToString();
                values[2] = attr.Length.ToString();
                values[3] = attr.isNull.ToString();
                mAttributesGrid.Rows.Add(values);

            }
            if (mAttributesGrid.Rows.Count > 0) mAttributesGrid.Rows[0].Selected = true;


            //mAttributes.Items.Clear();
            //if (SelectedTable() == null) return;
            //foreach (Attribute attr in SelectedTable().Attributes)
            //{
            //    mAttributes.Items.Add(attr.Name);
            //}
            //if (mAttributes.Items.Count > 0) mAttributes.SetSelected(0, true);
        }

        public void ReloadAll()
        {
            ReloadDatabases();
            ReloadTables();
            ReloadAttributes();
        }

        public void AddDatabase(string name)
        {

            if (mSysdatabases.CreateDB(name) == true)
            {
                mDatabases.Items.Add(name);
                mDatabases.SetSelected(mDatabases.Items.Count - 1,true);
            }
        }

        public void AddTable(string tablename)
        {
            if (mSysdatabases.OpenedDatabase == null) return;
            if (mSysdatabases.Systables.CreateTable(tablename)!=null)
            {
                mTables.Items.Add(tablename);
                mTables.SetSelected(mTables.Items.Count - 1, true);
            }
        }

        public void AddAttribute(string name,Attribute.AttributeTypes type, int length,bool isNull)
        {
            if (SelectedTable() == null) return;
            Attribute attr = null;
            if ((attr = mSysdatabases.Systables.AddAttr(SelectedTable().TableName, name, type, length, isNull)) != null)
            {
                object[] values = new object[4];
                values[0] = attr.Name;
                values[1] = attr.Type.ToString();
                values[2] = attr.Length.ToString();
                values[3] = attr.isNull.ToString();

                mAttributesGrid.Rows.Add(values);
                if (mAttributesGrid.Rows.Count > 0) mAttributesGrid.Rows[0].Selected = true;

            }
        }



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void attribToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mAddAttributeForm.Visible = true;
        }

        private void createToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mAddDatabaseForm.AddDatabase();
        }

        private void createToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            mAddTableForm.AddTable();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (mSysdatabases.OpenDB((string)mDatabases.SelectedItem))
            {
                mDatabases.Enabled = false;
                ReloadTables();
                ReloadAttributes();
            }


        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (mSysdatabases.OpenedDatabase != null)
            {
                System.Windows.Forms.MessageBox.Show("Close database first", "Warning");
                return;
            }

            mSysdatabases.DropDB((string)mDatabases.SelectedItem);
            ReloadDatabases();
            ReloadTables();
            ReloadAttributes();
        }

        private void deleteToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (mSysdatabases.OpenedDatabase == null) return;
            bool dropped = mSysdatabases.Systables.DropTab((string)mTables.SelectedItem);
            if (!dropped) return;

            int selected = mTables.SelectedIndex;
            if ((selected >= mTables.Items.Count)||(selected<0)) return;
            mTables.Items.RemoveAt(selected);
            if (selected >= mTables.Items.Count) return;
            mTables.SetSelected(selected, true);

        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (mSysdatabases.CloseDB((string)mDatabases.SelectedItem))
            {
                mDatabases.Enabled = true;
                ReloadTables();
                ReloadAttributes();
            }
        }

        private void creatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mAddAttributeForm.AddAttribute();
        }

        private void alterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Attribute sel = SelectedAttribute();
            if (sel != null)
            {
                int index = mAttributesGrid.SelectedRows[0].Index;
                mAlterAttributeForm.Show(sel);
                mAttributesGrid.SelectedRows[0].Cells[1].Value = sel.Type.ToString();
                mAttributesGrid.SelectedRows[0].Cells[2].Value = sel.Length.ToString();
                mAttributesGrid.SelectedRows[0].Cells[3].Value = sel.isNull.ToString();
                mSysdatabases.Systables.SysColumns.Save();
            }


        }

        private void deleteToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (SelectedAttribute()!=null)
            {

                int selected = mAttributesGrid.SelectedRows[0].Index;
                if (mSysdatabases.Systables.DropAtr(SelectedTable().TableName, SelectedAttribute().Name))
                {
                    mAttributesGrid.Rows.RemoveAt(selected);
                    if (mAttributesGrid.Rows.Count > selected) mAttributesGrid.Rows[selected].Selected = true;
                }
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void mTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            ReloadAttributes();
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void mAttributes_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void mAttributesGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}